package volatileDemo;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : SyncDemo
 * @Author : 54061
 * @Date: 2022/2/16 20:02
 */
public class SyncDemo {
    public static void main(String[] args) {

        //创建小红和小刚线程
        MyThread3 小红 = new MyThread3();
        MyThread4 小刚 = new MyThread4();


        小红.start();
        小刚.start();
    }
}
class Money1{
    //新new的锁对象
    public static Object lock = new Object();
    //共享数据
    public static int money = 100000;
}
//小红
class MyThread3 extends Thread{
    @Override
    public void run() {
        while (true){
            //上锁 :线程访问共享数据的时候优先访问内存中的共享数据而不是临时变量去
            synchronized (Money1.lock){
                System.out.println("======== = " );
                if (Money1.money !=100000){
                    System.out.println("结婚基金一局不是十万了");
                    break;
                }

            }
        }
    }
}
//小刚
class MyThread4 extends Thread{
    @Override
    public void run() {
        synchronized (Money1.lock){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Money1.money = 90000;
        }
    }
}
